/*
 * Copyright (c) 2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/opengl/GLShader.h"

namespace alkes {

GLShader* GLShader::create(const char* source, GLenum shader_type)
{
    if (!source)
        return 0;

    if (!(shader_type == GL_VERTEX_SHADER
        || shader_type == GL_FRAGMENT_SHADER))
    {
        LOGE("Invalid GL-shader type.(0x%04x)", shader_type);
        return 0;
    }

    GLuint shader;
    AL_OGL_INVOKE(shader = glCreateShader(shader_type));
    if (shader == 0)
    {
        LOGE("Failed to create shader.");
        return 0;
    }

    AL_OGL_INVOKE(glShaderSource(shader, 1, &source, 0));

    glCompileShader(shader);

    GLint compile_result;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_result);
    if (GL_FALSE == compile_result)
    {
        GLint info_len;
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_len);

        if (info_len > 0)
        {
            TExtendBuffer<> temp;
            GLchar* p_temp = reinterpret_cast<GLchar*>(temp.getBuffer(info_len));
            glGetShaderInfoLog(shader, info_len, 0, p_temp);
            LOGE("Shader compile error: %s", p_temp);
        }

        glDeleteShader(shader);
        return 0;
    }

    return new GLShader(
        shader,
        source,
        shader_type == GL_VERTEX_SHADER ? AL_SHADER_TYPE_VERTEX : AL_SHADER_TYPE_FRAGMENT);
}

GLShader* GLShader::createVertexShader(const char* source)
{
    return create(source, GL_VERTEX_SHADER);
}

GLShader* GLShader::createFragmentShader(const char* source)
{
    return create(source, GL_FRAGMENT_SHADER);
}

void GLShader::destroy()
{
    delete this;
}

GLShader::GLShader(GLuint shader, const char* source, ShaderType type)
: Shader(source, type)
, shader_(shader)
{
}

GLShader::~GLShader()
{
}

GLuint GLShader::getShader() const
{
    return shader_;
}

}
